//
//  PSLoginViewController.m
//  psico
//
//  Created by wagner silva on 09/07/13.
//  Copyright (c) 2013 ws. All rights reserved.
//

#import "PSLoginViewController.h"
#import "PSPerfilViewController.h"
#import "AESCrypt.h"

@interface PSLoginViewController ()

@end

@implementation PSLoginViewController

@synthesize txtEmail;
@synthesize txtSenha;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)recuarTeclado:(id)sender{
    [sender resignFirstResponder];

}

- (IBAction)clicarNoFundo:(id)sender{
    [txtEmail resignFirstResponder];
    [txtSenha resignFirstResponder];
}

- (IBAction)limparEmail:(id)sender {
    txtEmail.text=@"";
}

- (IBAction)limparSenha:(id)sender {
    txtSenha.text = @"";
}

- (IBAction)onClickLogin:(id)sender {
     if ([txtEmail.text isEqualToString:@""] || [txtSenha.text isEqualToString:@""]) {
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Preencha todos os campos" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
     [alert show];
     return;
     }
    
    //Encriptando a senha
    
    NSString *senha = txtSenha.text;
    NSString *chave = txtEmail.text;
    
    NSString *senhaEncriptada = [AESCrypt encrypt:senha password:chave];
    
    //Decriptando a senha
   // NSString *decripty = [AESCrypt decrypt:senhaEncriptada password:chave];
    
    // i will use a code from connect to DB tutorial
     NSString *strURL = [NSString stringWithFormat:@"http://192.168.254.233:8080/pslogin.php?email=%@&senha=%@",txtEmail.text.lowercaseString, senhaEncriptada];
     
     // to execute php code
     NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
    
    if (dataURL == nil)
    {
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Erro ao conectar ao banco de dados. Verifique sua rede" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        
         [alert show];
         
         return;
     }
    
     // to receive the returend value
    NSMutableString *strResult = [[NSMutableString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
     
     if ([strResult isEqualToString:@"1"])
     {
         PSPerfilViewController *janela = [[PSPerfilViewController alloc] initWithNibName: @"PSPerfilViewController" bundle:nil];
         
         [self presentModalViewController:janela animated:true];
     }
     else
     {
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Senha invalida" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
         [alert show];
         
         return;
     }
}

- (IBAction)onClickReenviarSenha:(id)sender {
	MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
	picker.mailComposeDelegate = self;
	
	[picker setSubject:@"Solicitacao de senha!"];
	
	// Set up recipients
	NSArray *toRecipients = [NSArray arrayWithObject:@"apppsique@gmail.com"];
	
	[picker setToRecipients:toRecipients];
	
	// Attach an image to the email
	NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
	NSData *myData = [NSData dataWithContentsOfFile:path];
	[picker addAttachmentData:myData mimeType:@"psic.png" fileName:@"psic"];
	
	// Fill out the email body text
	NSString *emailBody = @"Reenvio de senha";
    
	[picker setMessageBody:emailBody isHTML:NO];
	
	[self presentModalViewController:picker animated:YES];
	//[picker release];
}

- (IBAction)apertarTecla:(id)sender {
   txtEmail.text = txtEmail.text.lowercaseString;
}

@end
